package com.bsj.travel.def.common.DTO.mqtt;

import com.bsj.travel.util.StringUtils;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author yinhao
 * @version 1.0
 * @description MQTT设备消息上行消息传输对象
 * @date 2023/11/13
 */
@Data
@NoArgsConstructor
public class MqttEvent<T> implements Serializable {
    private static final long serialVersionUID = 8659759400941247496L;

    /**
     * 消息接收时间
     */
    private long timestamp;
    /**
     * 事件枚举
     */
    private String event;
    /**
     * 采用deviceName&productKey
     */
    private String client;
    /**
     * 消息通知主题
     */
    private String topic;
    /**
     * 消息等级
     */
    private int qos;
    /**
     * 消息内容
     */
    private T data;

    public MqttEvent(long timestamp, String clientId, String topic, int qoS, T message) {
        this.client = getClient(clientId);
        this.topic = topic;
        this.qos = qoS;
        this.data = message;
        this.timestamp = timestamp;
    }

    private static String getClient(String clientId) {
        if (StringUtils.isEmpty(clientId)) {
            return null;
        }
        String[] splitClient = clientId.split("\\|");
        //b1k9equuopu.test0001  为了跟mqtt连接username保持一致，需要将该值重新组合
        String[] split = splitClient[0].split("\\.");
        return split[1] + "&" + split[0];
    }
}
